using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKASTGA
    {
        public DataRow Convert2KASTGA(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKASTGA(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KGAMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KGAKANR"] = " ".PadRight(8 ); 
         dwReturn["KGAKAPOS"] = " ".PadRight(4 ); 
         dwReturn["KGAPSA"] = " ".PadRight(1 ); 
         dwReturn["KGAPOS"] = " ".PadRight(2 ); 
         dwReturn["KGAFPOS"] = " ".PadRight(0 ); 
         dwReturn["KGAART"] = " ".PadRight(20 ); 
         dwReturn["KGAVAR"] = " ".PadRight(3 ); 
         dwReturn["KGAFARBE"] = " ".PadRight(10 ); 
         dwReturn["KGAFDZ"] = " ".PadRight(0 ); 
         dwReturn["KGAABF"] = " ".PadRight(0 ); 
         dwReturn["KGAKGNET"] = " ".PadRight(0 ); 
         dwReturn["KGAKGBRUT"] = " ".PadRight(0 ); 
         dwReturn["KGAPART"] = " ".PadRight(5 ); 
         dwReturn["KGAZUSCHL1"] = " ".PadRight(8 ); 
         dwReturn["KGAZUSCHL2"] = " ".PadRight(8 ); 
         dwReturn["KGAZUSCHL3"] = " ".PadRight(8 ); 
         dwReturn["KGAZUSCHL4"] = " ".PadRight(8 ); 
         dwReturn["KGAZUSCHL5"] = " ".PadRight(8 ); 
         dwReturn["KGAZUSCHL6"] = " ".PadRight(8 ); 
         dwReturn["KGABPREIS"] = " ".PadRight(0 ); 
         dwReturn["KGAPREIS"] = " ".PadRight(0 ); 
         dwReturn["KGAKOSTEN"] = " ".PadRight(0 ); 
         dwReturn["KGAMGK1"] = " ".PadRight(0 ); 
         dwReturn["KGAMGK2"] = " ".PadRight(0 ); 
         dwReturn["KGAMGK3"] = " ".PadRight(0 ); 
         dwReturn["KGAMGK4"] = " ".PadRight(0 ); 
         dwReturn["KGAMGK5"] = " ".PadRight(0 ); 
         dwReturn["KGAGAAEND"] = " ".PadRight(1 ); 
         dwReturn["KGANETAEND"] = " ".PadRight(1 ); 
         dwReturn["KGAABFAEND"] = " ".PadRight(1 ); 
         dwReturn["KGAPRAEND"] = " ".PadRight(1 ); 
         dwReturn["KGAFREMD"] = " ".PadRight(1 ); 
         dwReturn["KGARECHNUM"] = " ".PadRight(4 ); 
         dwReturn["KGARECHNR"] = " ".PadRight(0 ); 
         dwReturn["KGAGARNBEZ"] = " ".PadRight(40 ); 
         dwReturn["KGAFILLER"] = " ".PadRight(96 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKASTGA(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKASTGA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKASTGA(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKASTGA(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKASTGA(dwLocal, ITXDBVERSION);
            }
            UpdateKASTGA(dwLocal);
        }
        public void InsertKASTGA(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KASTGA(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKASTGA(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KASTGA WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KASTGA  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KASTGA";
            Return = Result;
            return Return;
        }
        public void DeleteKASTGA(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KASTGA   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKASTGA(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KASTGA SET "
                    +"KGAMANDANT= :KGAMANDANT,"
                    +"KGAKANR= :KGAKANR,"
                    +"KGAKAPOS= :KGAKAPOS,"
                    +"KGAPSA= :KGAPSA,"
                    +"KGAPOS= :KGAPOS,"
                    +"KGAFPOS= :KGAFPOS,"
                    +"KGAART= :KGAART,"
                    +"KGAVAR= :KGAVAR,"
                    +"KGAFARBE= :KGAFARBE,"
                    +"KGAFDZ= :KGAFDZ,"
                    +"KGAABF= :KGAABF,"
                    +"KGAKGNET= :KGAKGNET,"
                    +"KGAKGBRUT= :KGAKGBRUT,"
                    +"KGAPART= :KGAPART,"
                    +"KGAZUSCHL1= :KGAZUSCHL1,"
                    +"KGAZUSCHL2= :KGAZUSCHL2,"
                    +"KGAZUSCHL3= :KGAZUSCHL3,"
                    +"KGAZUSCHL4= :KGAZUSCHL4,"
                    +"KGAZUSCHL5= :KGAZUSCHL5,"
                    +"KGAZUSCHL6= :KGAZUSCHL6,"
                    +"KGABPREIS= :KGABPREIS,"
                    +"KGAPREIS= :KGAPREIS,"
                    +"KGAKOSTEN= :KGAKOSTEN,"
                    +"KGAMGK1= :KGAMGK1,"
                    +"KGAMGK2= :KGAMGK2,"
                    +"KGAMGK3= :KGAMGK3,"
                    +"KGAMGK4= :KGAMGK4,"
                    +"KGAMGK5= :KGAMGK5,"
                    +"KGAGAAEND= :KGAGAAEND,"
                    +"KGANETAEND= :KGANETAEND,"
                    +"KGAABFAEND= :KGAABFAEND,"
                    +"KGAPRAEND= :KGAPRAEND,"
                    +"KGAFREMD= :KGAFREMD,"
                    +"KGARECHNUM= :KGARECHNUM,"
                    +"KGARECHNR= :KGARECHNR,"
                    +"KGAGARNBEZ= :KGAGARNBEZ,"
                    +"KGAFILLER= :KGAFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KGAMANDANT", OracleType.NVarChar).Value = dwData["KGAMANDANT"].ToString(); 
          or1.Parameters.Add("KGAKANR", OracleType.NVarChar).Value = dwData["KGAKANR"].ToString(); 
          or1.Parameters.Add("KGAKAPOS", OracleType.NVarChar).Value = dwData["KGAKAPOS"].ToString(); 
          or1.Parameters.Add("KGAPSA", OracleType.NVarChar).Value = dwData["KGAPSA"].ToString(); 
          or1.Parameters.Add("KGAPOS", OracleType.NVarChar).Value = dwData["KGAPOS"].ToString(); 
          or1.Parameters.Add("KGAFPOS", OracleType.NVarChar).Value = dwData["KGAFPOS"].ToString(); 
          or1.Parameters.Add("KGAART", OracleType.NVarChar).Value = dwData["KGAART"].ToString(); 
          or1.Parameters.Add("KGAVAR", OracleType.NVarChar).Value = dwData["KGAVAR"].ToString(); 
          or1.Parameters.Add("KGAFARBE", OracleType.NVarChar).Value = dwData["KGAFARBE"].ToString(); 
          or1.Parameters.Add("KGAFDZ", OracleType.NVarChar).Value = dwData["KGAFDZ"].ToString(); 
          or1.Parameters.Add("KGAABF", OracleType.NVarChar).Value = dwData["KGAABF"].ToString(); 
          or1.Parameters.Add("KGAKGNET", OracleType.NVarChar).Value = dwData["KGAKGNET"].ToString(); 
          or1.Parameters.Add("KGAKGBRUT", OracleType.NVarChar).Value = dwData["KGAKGBRUT"].ToString(); 
          or1.Parameters.Add("KGAPART", OracleType.NVarChar).Value = dwData["KGAPART"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL1", OracleType.NVarChar).Value = dwData["KGAZUSCHL1"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL2", OracleType.NVarChar).Value = dwData["KGAZUSCHL2"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL3", OracleType.NVarChar).Value = dwData["KGAZUSCHL3"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL4", OracleType.NVarChar).Value = dwData["KGAZUSCHL4"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL5", OracleType.NVarChar).Value = dwData["KGAZUSCHL5"].ToString(); 
          or1.Parameters.Add("KGAZUSCHL6", OracleType.NVarChar).Value = dwData["KGAZUSCHL6"].ToString(); 
          or1.Parameters.Add("KGABPREIS", OracleType.NVarChar).Value = dwData["KGABPREIS"].ToString(); 
          or1.Parameters.Add("KGAPREIS", OracleType.NVarChar).Value = dwData["KGAPREIS"].ToString(); 
          or1.Parameters.Add("KGAKOSTEN", OracleType.NVarChar).Value = dwData["KGAKOSTEN"].ToString(); 
          or1.Parameters.Add("KGAMGK1", OracleType.NVarChar).Value = dwData["KGAMGK1"].ToString(); 
          or1.Parameters.Add("KGAMGK2", OracleType.NVarChar).Value = dwData["KGAMGK2"].ToString(); 
          or1.Parameters.Add("KGAMGK3", OracleType.NVarChar).Value = dwData["KGAMGK3"].ToString(); 
          or1.Parameters.Add("KGAMGK4", OracleType.NVarChar).Value = dwData["KGAMGK4"].ToString(); 
          or1.Parameters.Add("KGAMGK5", OracleType.NVarChar).Value = dwData["KGAMGK5"].ToString(); 
          or1.Parameters.Add("KGAGAAEND", OracleType.NVarChar).Value = dwData["KGAGAAEND"].ToString(); 
          or1.Parameters.Add("KGANETAEND", OracleType.NVarChar).Value = dwData["KGANETAEND"].ToString(); 
          or1.Parameters.Add("KGAABFAEND", OracleType.NVarChar).Value = dwData["KGAABFAEND"].ToString(); 
          or1.Parameters.Add("KGAPRAEND", OracleType.NVarChar).Value = dwData["KGAPRAEND"].ToString(); 
          or1.Parameters.Add("KGAFREMD", OracleType.NVarChar).Value = dwData["KGAFREMD"].ToString(); 
          or1.Parameters.Add("KGARECHNUM", OracleType.NVarChar).Value = dwData["KGARECHNUM"].ToString(); 
          or1.Parameters.Add("KGARECHNR", OracleType.NVarChar).Value = dwData["KGARECHNR"].ToString(); 
          or1.Parameters.Add("KGAGARNBEZ", OracleType.NVarChar).Value = dwData["KGAGARNBEZ"].ToString(); 
          or1.Parameters.Add("KGAFILLER", OracleType.NVarChar).Value = dwData["KGAFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
